﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BuXiaChu.OrderDishes.BLL;
using BuXiaChu.OrderDishes.Web.Admin.Modules.ShareCode;
using BuXiaChu.OrderDishes.BLL.WebRuntime;
using BuXiaChu.OrderDishes.Model;

namespace BuXiaChu.OrderDishes.Web.Admin.ShareCode
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ShareCode : HttpHandlerBase, IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            base.Init(context);
            var type = Request.Form["t"];
            switch (type)
            {
                case "1":
                    GetShareCodeList();
                    break;
                case "2":
                    Add();
                    break;
                case "3":
                    Update();
                    break;
                case "4":
                    Delete();
                    break;
                default: break;
            }
        }

        public void GetShareCodeList()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            UserControlPreviewManager<ShareCodeList> preivewManager = new UserControlPreviewManager<ShareCodeList>();
            ShareCodeList shareCodeList = preivewManager.LoadUserControl("~/Modules/ShareCode/ShareCodeList.ascx");
            shareCodeList.List = ShareCodeManager.GetListByParentIDandClassId(long.Parse(Request.Form["parentId"]), long.Parse(Request.Form["classId"]));

            result.message = preivewManager.RenderHtml(shareCodeList);
            result.success = true;
            SerializeDataToResponseStream(result);
            #endregion
        }

        public void Add()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            long parentId = 0, classId = 0;
            if (long.TryParse(Request.Form["parentId"], out parentId)
                && long.TryParse(Request.Form["classId"], out classId)
                && !string.IsNullOrEmpty(Request.Form["codeName"]))
            {
                ShareCodeModel entity = new ShareCodeModel();
                entity.ParentId = parentId;
                entity.ClassID = classId;
                entity.Name = Request.Form["codeName"].Trim();

                result.success = ShareCodeManager.Add(entity);
            }
            SerializeDataToResponseStream(result);
            #endregion
        }

        public void Update()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            long parentId = 0, classId = 0, id = 0;
            if (long.TryParse(Request.Form["parentId"], out parentId)
                && long.TryParse(Request.Form["classId"], out classId)
                && !string.IsNullOrEmpty(Request.Form["codeName"])
                && long.TryParse(Request.Form["id"], out id))
            {
                ShareCodeModel entity = new ShareCodeModel();
                entity.ParentId = parentId;
                entity.ClassID = classId;
                entity.Name = Request.Form["codeName"].Trim();
                entity.CodeID = id;

                result.success = ShareCodeManager.Add(entity);
            }
            SerializeDataToResponseStream(result);
            #endregion
        }

        public void Delete()
        {
            #region
            var id = 0;
            AjaxActionResult result = new AjaxActionResult();
            if (int.TryParse(Request.Form["id"], out id))
            {
                result.success = ShareCodeManager.DeleteShareCode(id);
            }

            SerializeDataToResponseStream(result);
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
